local root = getRootElement()
local rootElement = getResourceRootElement ( getThisResource() )
local aInfo = {}

function resourceStart()
	local players = getElementsByType ( "player" )
	if getElementType ( source ) == "player" then
		killPed ( source )
		fadeCamera ( source, false, 0, 100, 100, 100 )
		setElementPosition ( source, 1661, 1344, 11 )
		setCameraMatrix ( source, 2051.75, 1196.75, 84.75, 2351.25, 1404.5, 0, 0, 180 )
		addEvent ( "loggedIn", true )
		addEventHandler ( "loggedIn", source, displayCharacter )
	else
		for k,v in ipairs ( players ) do
			killPed ( v )
			fadeCamera ( v, false, 0, 100, 100, 100 )
			setElementPosition ( v, 1661, 1344, 11 )
			setCameraMatrix ( v, 2051.75, 1196.75, 84.75, 2351.25, 1404.5, 0, 0, 180 )
			addEvent ( "loggedIn", true )
			addEventHandler ( "loggedIn", v, displayCharacter )
		end
	end
end

function clientReady()
	triggerClientEvent ( "showLoginWindow", source )
end

function retrievePlayerNames()
	local names = executeSQLSelect ( "character_names", "name" )
	triggerClientEvent ( "sendPlayerNames", source, names )
end

function loadCharacters ( player )
	local account = getPlayerAccount ( player )
	aInfo[account] = {}
	for i=1,8 do
		if ( getAccountData ( account, ("charname"..i ) ) == false ) then
			aInfo[account][i] = {}
			aInfo[account][i][1] = "Empty Slot"
			aInfo[account][i][2] = "N/a"
			aInfo[account][i][3] = "N/a"
			aInfo[account][i][4] = "N/a"
			aInfo[account][i][5] = "N/a"
			aInfo[account][i][6] = "N/a"
			aInfo[account][i][7] = "N/a"
			aInfo[account][i][8] = "N/a"
		else
			aInfo[account][i] = {}
			aInfo[account][i][1] = getAccountData ( account, ("charname"..i ) )
			aInfo[account][i][2] = getAccountData ( account, ("charskin"..i ) )
			aInfo[account][i][3] = getAccountData ( account, ("money"..i ) )
			aInfo[account][i][4] = getAccountData ( account, ("threat"..i ) )
			aInfo[account][i][5] = getAccountData ( account, ("level"..i ) )
			aInfo[account][i][6] = getAccountData ( account, ("vslot"..i ) )
			aInfo[account][i][7] = getAccountData ( account, ("wslot1"..i ) )
			aInfo[account][i][8] = getAccountData ( account, ("wslot2"..i ) )
		end
		triggerClientEvent ( player, "sendPlayerData", player, i, aInfo[account][i][1], aInfo[account][i][2], aInfo[account][i][3], aInfo[account][i][4], aInfo[account][i][5], aInfo[account][i][6], aInfo[account][i][7], aInfo[account][i][8] )
	end
	triggerClientEvent ( "displayCharacters", player )
end

function displayCharacter()
	triggerClientEvent ( "displayCharacterSelect", source )
	loadCharacters ( source )
	setTimer ( fadeCamera, 1000, 1, source, true, 2 )
end

function buildCharacter ( slot, name, skin, faction )
	local account = getPlayerAccount ( source )
	aInfo[account] = {}
	aInfo[account][slot] = {}
	setAccountData ( account, ("charname"..slot ), name )
	setAccountData ( account, ("charskin"..slot ), tostring ( skin ) )
	setAccountData ( account, ("money"..slot ), "0" )
	setAccountData ( account, ("threat"..slot ), "gr" )
	setAccountData ( account, ("level"..slot ), "0" )
	setAccountData ( account, ("vslot"..slot ), "Starter Car" )
	setAccountData ( account, ("wslot1"..slot ), "Starter Primary" )
	setAccountData ( account, ("wslot2"..slot ), "Starter Secondary" )
end

addEventHandler ( "onResourceStart", rootElement, resourceStart )
addEventHandler ( "onPlayerJoin", root, resourceStart )
addEvent ( "requestPlayerNames", true )
addEventHandler ( "requestPlayerNames", getRootElement(), retrievePlayerNames )
addEvent ( "buildCharacter", true )
addEventHandler ( "buildCharacter", getRootElement(), buildCharacter )
addEvent ( "clientReady", true )
addEventHandler ( "clientReady", getRootElement(), clientReady )